package com.ce.pms.occupation.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.occupation.dao.IPerfectItemDispatchDao;
import com.ce.pms.occupation.dao.IPerfectItemRequisitionDao;
import com.ce.pms.occupation.model.PerfectItemDispatchDO;
import com.ce.pms.occupation.model.PerfectItemRequisitionDO;
import com.ce.pms.occupation.service.IDispatchExportService;
import com.ce.util.BlankUtil;
/**
 * 功能描述: 入伙派工导出类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2009-1-5
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class DispatchExportServiceImpl extends PmsBaseServiceImpl implements
		IDispatchExportService {
	/**
	 * 方法描述: 派工单完工状态验证方法
	 * 已经完工则不允许导出
	 * @author 胡煦
	 * Create on:2009-1-21
	 * @param dispatchId
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkDispatchIsComp(Long dispatchId){
		PerfectItemDispatchDO dispatchDO = perfectItemDispatchDao.findById(dispatchId);
		if(checkIsHasCompletedItem(dispatchDO.getPerfectItemRequisitions())){
			throw new BusinessException("occupation:occupation_dispatch_exportandimport_exception", BusinessException.WARNING);
		}
	}
	/**
	 * 方法描述: 验证整改派工是否存在已经完工的项目
	 * @author 胡煦
	 * Create on:2009-1-4
	 * @param set
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private boolean checkIsHasCompletedItem(Set<PerfectItemRequisitionDO> set){
		if(set.size() < 1){
			return false;
		}
		for(PerfectItemRequisitionDO itemRequisitionDO : set){
			if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(itemRequisitionDO.getCompleteYn())){
				return true;
			}
		}
		return false;
	}
	/**
	 * 方法描述: 创建单个的HSSFSheet头
	 * @author 胡煦
	 * Create on:2008-8-26
	 * @param HSSFRow row,HSSFCell cell
	 * @return HSSFSheet
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
    private HSSFCell creatSheetHead(HSSFRow row,int i, HSSFWorkbook workbook){
    	HSSFCell cell;
    	//创建一个样式
        HSSFCellStyle style = workbook.createCellStyle();
        //style.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER ); //居中对齐
        HSSFFont font = workbook.createFont();
     	// 设置字体颜色
     	font.setColor(HSSFFont.COLOR_RED);
     	// 设置字体大小
     	font.setFontHeight((short)240);
     	font.setBoldweight((short)480);
     	style.setFont(font);
     	//设置表头
     	cell = row.createCell((short) i++);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	    cell.setCellStyle(style);
		cell.setCellValue("项目编号");
		cell = row.createCell((short) i++);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	    cell.setCellStyle(style);
		cell.setCellValue("房号");
		cell = row.createCell((short) i++);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellStyle(style);
		cell.setCellValue("项目类型");
		cell = row.createCell((short) i++);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellStyle(style);
		cell.setCellValue("问题描述");
		cell = row.createCell((short) i++);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellStyle(style);
		cell.setCellValue("完工日期");
		cell = row.createCell((short) i++);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellStyle(style);
		cell.setCellValue("备注");
		return cell;
    }
	/**
	 * 方法描述: 创建单个的HSSFSheet
	 * @author 胡煦
	 * Create on:2008-8-26
	 * @param List,ProjectDomain,BuildingDomain,meterType,workbook
	 * @return HSSFSheet
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
    private HSSFSheet creatSheet(List<Object[]> list, HSSFWorkbook workbook) {

    	//创建一个样式
        HSSFCellStyle style = workbook.createCellStyle();
        //style.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直对齐
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐
        HSSFFont font = workbook.createFont();
     	// 设置字体大小
     	font.setFontHeight((short)220);
     	font.setBoldweight((short)400);
     	style.setFont(font);

		// 新建工作表，其语句为：
		HSSFSheet sheet = workbook.createSheet();
		//设置列宽
		sheet.setColumnWidth((short) 0,(short)(( 42*5)/((double)1/20))) ;
		sheet.setColumnWidth((short) 6,(short)(( 38*5)/((double)1/20))) ;
		// 在索引0的位置创建行（最顶端的行）
		HSSFRow row = sheet.createRow((short) 0);
		// 在索引0的位置创建单元格（左上端）
		int i = 0;
		//创建表头
		HSSFCell cell = this.creatSheetHead(row,i,workbook);
		// = row.createCell((short) 0);
		// 定义单元格为字符串类型
		// cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		// 设置cell编码解决中文高位字节截断
		// cell.setEncoding(HSSFCell.ENCODING_UTF_16);

		// 下面是不断从数据库中读数据到sheet中


		for (int j = 0; j < list.size(); j++) {
			//ChargeDatasDomain chargeDatasDomain = chargeDataslist.get(j);
			Long dispatchId = (Long) list.get(j)[0];
			String houseNumber = (String) list.get(j)[1];
			String perItemTypeName = (String) list.get(j)[2];
			String description = (String) list.get(j)[3];
			i = 0;

			row = sheet.createRow((short) j + 1);
			// 设置项目编号
			cell = row.createCell((short) i++);
			cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellStyle(style);
			cell.setCellValue(dispatchId);
			// 设置房号
			cell = row.createCell((short) i++);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellStyle(style);
			cell.setCellValue(houseNumber);
			// 设置项目类型
			cell = row.createCell((short) i++);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellStyle(style);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			//flatDomain = (FlatDomain) chargeDatasDomain.getHouse();
			cell.setCellValue(perItemTypeName);
			// 设置问题描述
			cell = row.createCell((short) i++);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellStyle(style);
			cell.setCellValue(description);

			// 设置空的完工日期
			cell = row.createCell((short) i++);
			HSSFCellStyle style1 = workbook.createCellStyle();
			//cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		    style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直对齐
		    style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐
		    style1.setFont(font);
			cell.setCellStyle(style1);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("");

		}
		return sheet;
	}

    private List<Object[]> createBaseData(Long perfectItemDispatchId){


    	List<Object[]> list = new ArrayList<Object[]>();

    	List<PerfectItemRequisitionDO> perfectItemsList = perfectItemRequisitionDao.getPerItemsByDispatchId(perfectItemDispatchId);
    	if(perfectItemsList.size() > 0 ){
    		for(PerfectItemRequisitionDO itemRequisitionDO : perfectItemsList){

    			Object[] obj = new Object[]{itemRequisitionDO.getKeyword(),itemRequisitionDO.getHouseNumber(),itemRequisitionDO.getPerfectItemTypeConfig().getPerfectItemTypeName(),itemRequisitionDO.getDescription()};

    			list.add(obj);
    		}
    	}

    	return list;
    }

    /**
	 * 方法描述: 创建工作簿
	 * @author 胡煦
	 * Create on:2008-8-26
	 * @param Long perfectItemDispatchId
	 * @return HSSFWorkbook
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
    public HSSFWorkbook createHSSFWorkbook(Long perfectItemDispatchId){

    		List<Object[]> list = this.createBaseData(perfectItemDispatchId);
			// 创建新的Excel 工作簿
			HSSFWorkbook workbook = new HSSFWorkbook();

			if(!BlankUtil.isBlank(list)){
				HSSFSheet sheet = this.creatSheet(list,workbook);
			}
			return workbook;
	}
    /**
	 * 方法描述: 创建工作簿并导出
	 * @author 胡煦
	 * Create on:2008-8-26
	 * @param Long perfectItemDispatchId
	 * @return HSSFWorkbook
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
    public HSSFWorkbook exportExcel(Long perfectItemDispatchId){
    	checkDispatchIsComp(perfectItemDispatchId);
    	//获取要导出的派工单实体
    	PerfectItemDispatchDO dispatchDO = perfectItemDispatchDao.findById(perfectItemDispatchId);
    	HSSFWorkbook workbook = this.createHSSFWorkbook(perfectItemDispatchId);
    	//将派工单的状态置为已导出并保存
    	dispatchDO.setExpStatus(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
    	perfectItemDispatchDao.saveOrUpdateObject(dispatchDO);
    	return workbook;
    }

    private IPerfectItemRequisitionDao perfectItemRequisitionDao;
    private IPerfectItemDispatchDao perfectItemDispatchDao;
	/**
	 * @param perfectItemRequisitionDao the perfectItemRequisitionDao to set
	 */
	public void setPerfectItemRequisitionDao(
			IPerfectItemRequisitionDao perfectItemRequisitionDao) {
		this.perfectItemRequisitionDao = perfectItemRequisitionDao;
	}
	/**
	 * @param perfectItemDispatchDao the perfectItemDispatchDao to set
	 */
	public void setPerfectItemDispatchDao(
			IPerfectItemDispatchDao perfectItemDispatchDao) {
		this.perfectItemDispatchDao = perfectItemDispatchDao;
	}
}
